import { request } from 'src/utils';

/**
 * 登录表单
 */
export interface LoginForm {
  username: string;
  password: string;
  captcha?: string | null;
  captchaKey?: string | null;
}

/**
 * 验证码接口
 */
export interface Captcha {
  enabled: boolean;
  captcha: string;
  captchaKey: string;
}

/**
 * 用户信息
 */
export interface LoginUser {
  username: string;
  nickname: string;
  avatar: string;
  roles: string[];
  permissions: string[];
  menus: MenuVO[];
}

export interface RouteMenu {
  name: string;
  path: string;
  component?: string;
  redirect?: string;
  meta: RouteMeta;
  children: RouteMenu[];
}

export interface RouteMeta {
  title: string;
  icon?: string;
  hidden: boolean;
  noCache: boolean;
}

export interface MenuVO {
  path: string; // 路径，必须且唯一
  title: string; // 名称，必须
  icon?: string; // 图标
  isLink?: boolean; // 是否链接
  children?: MenuVO[]; // 子菜单
}

/**
 * 登录系统
 */
export function doLogin(data: LoginForm): Promise<string> {
  return request.post('/login', data);
}

/**
 * 退出系统
 */
export function doLogout(): Promise<void> {
  return request.post('/logout');
}

/**
 * 获取登录用户详情
 */
export function getLoginUser(): Promise<LoginUser> {
  return request.get('/userinfo');
}

/**
 * 获取验证码
 */
export function getCaptcha(): Promise<Captcha> {
  return request.get('/captcha');
}

export default { doLogin, doLogout, getCaptcha, getLoginUser };
